package com.taiji.dicom4cloud.smart.alarm.utils;

import cn.hutool.core.util.HexUtil;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;

/**
 * 预警消息处理
 *
 * @Description
 * @Author fanxl
 * @Date 2021/8/23
 **/
public class MessageUtils {

    /**
     * 开机
     */
    public static byte[] START = {0x00,0x01};
    /**
     * 正常工作
     */
    public static byte[] NORMAL = {0x00,0x02};
    /**
     * 正常工作
     */
    public static byte[] REAL_DATA = {0x00,0x03};
    /**
     * 报警
     */
    public static byte[] REPORT_ALARM = {0x00,0x04};

    /**
     * 预警
     */
    public static byte[] ALARM = {0x00,0x05};
    /**
     * 故障
     */
    public static byte[] BREAK_DOWN = {0x00,0x06};

    /**
     * 预警恢复
     */
    public static byte[] ALARM_RECOVERY = {0x00,0x07};

    /**
     * 预警恢复
     */
    public static byte[] BREAK_DOWN_RECOVERY = {0x00,0x08};

    //恶性负载
    public static  int BAD_LOAD = -32909;

    /**
     * 充电设备/恶性负载/恶性调压器检测报警数据
     */
    public static byte[] MALIGNANT_ALARM = {0x00,0x10};


    /**
     * 获取消息头部
     *
     * @param message
     * @return
     */
    public static byte[] getHeader(byte[] message) {
        return Arrays.copyOfRange(message, 0, 2);
    }

    /**
     * 获取版本号
     *
     * @param message
     * @return
     */
    public static byte[] getVersion(byte[] message) {
        return Arrays.copyOfRange(message, 2, 3);
    }

    /**
     * 获取序列号
     *
     * @param message
     * @return
     */
    public static byte[] getSeriesNo(byte[] message) {
        return Arrays.copyOfRange(message, 3, 5);
    }

    /**
     * 获取设备id
     *
     * @param message
     * @return
     */
    public static byte[] getEnquipmentId(byte[] message) {
        return Arrays.copyOfRange(message, 5, 15);
    }

    /**
     * 获取设备类型
     *
     * @param message
     * @return
     */
    public static byte[] getEnquipmentType(byte[] message) {
        return Arrays.copyOfRange(message, 15, 17);
    }

    /**
     * 获取加密字段
     *
     * @param message
     * @return
     */
    public static byte[] getEnc(byte[] message) {
        return Arrays.copyOfRange(message, 17, 18);
    }

    /**
     * 获取消息id
     *
     * @param message
     * @return
     */
    public static byte[] getId(byte[] message) {
        return Arrays.copyOfRange(message, 18, 20);
    }

    /**
     * 获取消息id
     * @param message
     * @return
     */
    public static Short getShortId(byte[] message) {
        return bytesToShort(Arrays.copyOfRange(message, 18, 20));
    }

    /**
     * 获取数据长度
     *
     * @param message
     * @return
     */
    public static short getDataLength(byte[] message) {
        return bytesToShort(Arrays.copyOfRange(message, 20, 22));
    }

    /**
     * 获取数据体
     *
     * @param message
     * @return
     */
    public static byte[] getData(byte[] message) {
        return Arrays.copyOfRange(message, 22, 22 + getDataLength(message));
    }

    /**
     * 获取校验
     * @param message
     * @return
     */
    public static short getCheck(byte[] message) {
        return bytesToShort(Arrays.copyOfRange(message, 22 + getDataLength(message), 24+getDataLength(message)));
    }

    /**
     * 获取结束符号
     * @param message
     * @return
     */
    public static short getEnd(byte[] message) {
        return bytesToShort(Arrays.copyOfRange(message, 24 + getDataLength(message), 26+getDataLength(message)));
    }

    /**
     * 数组转short
     * @param bytes
     * @return
     */
    public static short bytesToShort(byte[] bytes) {
        return ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).getShort();
    }
    /**
     * 数组是否相等
     *
     * @param b1
     * @param b2
     * @return
     */
    public static boolean compere(byte[] b1, byte[] b2) {

//1：
        if(b1.length == 0 || b2.length == 0 ){
            return false;
        }

//2：
        if (b1.length != b2.length) {
            return false;
        }

//3：
        boolean isEqual = true;

        for (int i = 0; i < b1.length && i < b2.length; i++) {
            if (b1[i] != b2[i]) {
                isEqual = false;
                break;
            }
        }
        return isEqual;
    }

    public static char[] hexToInt(byte[]data){
        return HexUtil.encodeHex(data);
    }

    public static void main(String[] args) {
     /*   byte[] message = {0x01, 0x02, 0x03, 0x05, 0x06};

        byte[] ss = getHeader(message);
        byte[] ss2 = getVersion(message);
        byte[] ss3 = getSeriesNo(message);

        byte dd[]= {0x09};
        char[] dd2  = hexToInt(dd);
        System.out.println("111");*/
        short b = -141;

        short s =  -115;
        byte[] message = {0x11, 0x02};
        short dd = bytesToShort(message);


        System.out.println(b);

    }

}
